package com.micro.modual.service.impl;

import com.micro.common.MicroResponseCode;
import com.micro.common.util.JacksonUtil;
import com.micro.core.exception.BizException;
import com.micro.core.frame.service.impl.AbstractBasicsServiceImpl;
import com.micro.core.resp.RespJson;
import com.micro.core.resp.RespJsonData;
import com.micro.modual.entity.GoodsCategory;
import com.micro.modual.entity.GoodsParameters;
import com.micro.modual.entity.GoodsSpec;
import com.micro.modual.entity.GoodsTemplate;
import com.micro.modual.extension.GoodsTemplateExtension;
import com.micro.modual.mapper.GoodsCategoryMapper;
import com.micro.modual.mapper.GoodsParametersMapper;
import com.micro.modual.mapper.GoodsSpecMapper;
import com.micro.modual.mapper.GoodsTemplateMapper;
import com.micro.modual.model.bo.goodstemplate.GoodsTemplateBO;
import com.micro.modual.model.dto.goodstemplate.GoodsSpecParamsDTO;
import com.micro.modual.service.GoodsTemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
*  <h1>商品模版表,用于关联商品规格,参数,并为商品类目做快速入口  实现</h1>
 * @author LevelCoder
 * @version V1.0.0
 * @email LevelCoder@126.com
 * @date 2020-08-04 00:11:10
 */
@Service("goodsTemplateService")
public class GoodsTemplateServiceImpl extends AbstractBasicsServiceImpl<GoodsTemplate,Long> implements GoodsTemplateService {

    @Autowired
    private GoodsTemplateMapper goodsTemplateMapper;

    @Autowired
    private GoodsCategoryMapper goodsCategoryMapper;

    @Autowired
    private GoodsParametersMapper goodsParametersMapper;

    @Autowired
    private GoodsSpecMapper goodsSpecMapper;
    
    /**
     * <h2>新增商品模版表,用于关联商品规格,参数,并为商品类目做快速入口详情</h2>
     * @param goodsTemplateBO {@link GoodsTemplateBO} 商品模版表,用于关联商品规格,参数,并为商品类目做快速入口 新增/修改 请求对象
     * @return {@link RespJson} 返回结果基础类
     */
    @Override
    public RespJson addGoodsTemplate(GoodsTemplateBO goodsTemplateBO) {
        GoodsTemplate goodsTemplate = GoodsTemplateExtension.converAddGoodsTemplate(goodsTemplateBO);
        goodsTemplateMapper.insertSelective(goodsTemplate);
        return new RespJson(MicroResponseCode.SUCCESS.getResponseCode(), MicroResponseCode.SUCCESS.getResponseMessage());
    }


    /**
     * <h2>根据主键编号删除商品模版表,用于关联商品规格,参数,并为商品类目做快速入口信息</h2>
     * @param id  商品模版表,用于关联商品规格,参数,并为商品类目做快速入口 主键编号
     * @return  {@link RespJson} 返回结果基础类
     */

    @Override
    public RespJson deleteGoodsTemplate(Long id) {
        GoodsTemplate goodsTemplate = goodsTemplateMapper.selectByPrimaryKey(id);
        if(goodsCategoryMapper.findCategoryCountByTemplateCode(goodsTemplate.getTemplateCode()) > 0){
            throw new BizException(MicroResponseCode.TEMPLATE_HAS_DISTRIBUTION_CATEGORY.getResponseCode(),MicroResponseCode.TEMPLATE_HAS_DISTRIBUTION_CATEGORY.getResponseMessage());
        }
        if(goodsParametersMapper.findParameterCountByTemplateCode(goodsTemplate.getTemplateCode()) > 0){
            throw new BizException(MicroResponseCode.TEMPLATE_HAS_DISTRIBUTION_PARAMETER.getResponseCode(),MicroResponseCode.TEMPLATE_HAS_DISTRIBUTION_PARAMETER.getResponseMessage());
        }
        if(goodsSpecMapper.findSpecCountByTemplateCode(goodsTemplate.getTemplateCode()) > 0){
            throw new BizException(MicroResponseCode.TEMPLATE_HAS_DISTRIBUTION_SPEC.getResponseCode(),MicroResponseCode.TEMPLATE_HAS_DISTRIBUTION_SPEC.getResponseMessage());
        }
        goodsTemplateMapper.deleteByPrimaryKey(id);
        return new RespJson(MicroResponseCode.SUCCESS.getResponseCode(),MicroResponseCode.SUCCESS.getResponseMessage());
    }

    @Override
    public RespJson validataTemplateExist(String templateName) {
        if(goodsTemplateMapper.findTemplateExist(templateName) > 0 ){
            throw new BizException(MicroResponseCode.GOODS_TEMPLATE_HAS_EXIST.getResponseCode(), MicroResponseCode.GOODS_TEMPLATE_HAS_EXIST.getResponseMessage());
        }else{
            return new RespJson(MicroResponseCode.SUCCESS.getResponseCode(),MicroResponseCode.SUCCESS.getResponseMessage());
        }
    }

    @Override
    public RespJsonData<GoodsSpecParamsDTO> findTemplateRelationInfo(String categoryCode) {
        GoodsCategory goodsCategory = goodsCategoryMapper.findGoodsCateGoryByCode(categoryCode);
        GoodsTemplate goodsTemplate = goodsTemplateMapper.findTemplateByCode(goodsCategory.getTemplateCode());
        //根据模版编号查询所有关联的属性列表
        List<GoodsSpec> goodsSpecList = goodsSpecMapper.findSpecListByTemplateCode(goodsTemplate.getTemplateCode());
        List<GoodsParameters> goodsParametersList = goodsParametersMapper.findParameterListByTemplateCode(goodsTemplate.getTemplateCode());
        GoodsSpecParamsDTO goodsSpecParams = GoodsTemplateExtension.converGoodsSpecParamsDTO(goodsSpecList,goodsParametersList);
        return new RespJsonData<GoodsSpecParamsDTO>(goodsSpecParams,MicroResponseCode.SUCCESS.getResponseCode(),MicroResponseCode.SUCCESS.getResponseMessage());

    }

}
